﻿using System;
using System.Collections.Generic;
using System.Text;
using Entity;
using DataAccess;
using DAL;
using System.Data;

namespace BLL
{
    /// <summary>
    /// 商品业务逻辑类
    /// </summary>
    public class GoodsBLL
    {
        private GoodsDAL goodsDAL = null;

        public GoodsBLL()
        {
            if (goodsDAL == null)
                goodsDAL = new GoodsDAL();
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Insert(Goods entity)
        {
            int result = 0;
            try
            {
                DataSourceTransactionManager.Open();
                DataSourceTransactionManager.BeginTransaction();

                //业务操作
                result=goodsDAL.Insert(entity);

                DataSourceTransactionManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                DataSourceTransactionManager.RollBackTransaction();
                throw new Exception(ex.Message);
            }
            finally
            {
                DataSourceTransactionManager.Close();
            }
            return result;
        }

        /// <summary>
        /// 插入
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public int Update(Goods entity)
        {
            int result = 0;
            try
            {
                DataSourceTransactionManager.Open();
                DataSourceTransactionManager.BeginTransaction();

                //业务操作
                result = goodsDAL.Update(entity);

                DataSourceTransactionManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                DataSourceTransactionManager.RollBackTransaction();
                throw new Exception(ex.Message);
            }
            finally
            {
                DataSourceTransactionManager.Close();
            }
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Delete(int id)
        {
            int result = 0;
            try
            {
                DataSourceTransactionManager.Open();
                DataSourceTransactionManager.BeginTransaction();

                //业务操作
                result = goodsDAL.Delete(id);

                DataSourceTransactionManager.CommitTransaction();
            }
            catch (Exception ex)
            {
                DataSourceTransactionManager.RollBackTransaction();
                throw new Exception(ex.Message);
            }
            finally
            {
                DataSourceTransactionManager.Close();
            }
            return result;
        }

        /// <summary>
        /// 获得商品列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetGoodsList()
        {
            DataTable dt=null;
            try
            {
                DataSourceTransactionManager.Open();
                dt = goodsDAL.GetGoodsList();
            }
            catch (Exception ex)
            {
                DataSourceTransactionManager.RollBackTransaction();
                throw new Exception(ex.Message);
            }
            finally
            {
                DataSourceTransactionManager.Close();
            }
            return dt;
        }

         /// <summary>
        /// 查找某一个商品
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Goods Find(int id)
        {
            Goods entity = null;
            try
            {
                DataSourceTransactionManager.Open();
                entity = goodsDAL.Find(id);
            }
            catch (Exception ex)
            {
                DataSourceTransactionManager.RollBackTransaction();
                throw new Exception(ex.Message);
            }
            finally
            {
                DataSourceTransactionManager.Close();
            }
            return entity;
        }
    }
}
